package com.ly.dingtalk.service.message.impl;


import static com.ly.dingtalk.constant.DingUrlConstant.WorkNotify.SEND_WORK_NOTIFY;

import java.util.List;

import org.jetbrains.annotations.NotNull;

import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiMessageCorpconversationGetsendprogressRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationGetsendresultRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationStatusBarUpdateRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendprogressResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendresultResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationStatusBarUpdateResponse;
import com.google.common.base.Preconditions;
import com.ly.dingtalk.component.application.IDingMiniH5;
import com.ly.dingtalk.constant.DingUrlConstant;
import com.ly.dingtalk.pojo.message.worknotify.IDingWorkNotifyMsg;
import com.ly.dingtalk.service.AbstractDingNewApi;
import com.ly.dingtalk.service.message.IDingWorkNotifyMessageHandler;
import com.ly.dingtalk.service.message.IDingWorkNotifyMessageHandler.StatusBgColor;

import cn.hutool.core.collection.CollUtil;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 钉钉工作通知
 * @author wangzhen
 */
@Slf4j
public class DingWorkNotifyMessageHandler extends AbstractDingNewApi implements IDingWorkNotifyMessageHandler {

    public DingWorkNotifyMessageHandler(IDingMiniH5 app) {
        super(app);
    }

    @Override
    public OapiMessageCorpconversationAsyncsendV2Response sendWorkNotifyToUsers(@NotNull List<String> userIds, @NotNull IDingWorkNotifyMsg msg) {
        return sendWorkNotify(userIds, null, false, msg);
    }
    
    /**
     * 
     * @Description: 只能发送给少于5个的指定的人, 不能发送给所有人
     * @see com.ly.dingtalk.service.message.IDingWorkNotifyMessageHandler#sendWorkNotify(java.util.List, java.util.List, boolean, com.ly.dingtalk.pojo.message.worknotify.IDingWorkNotifyMsg)
     * @date 2023-12-27 03:06:39
     */
    @Override
    public OapiMessageCorpconversationAsyncsendV2Response sendWorkNotify(@NotNull List<String> userIds, List<String> deptIds, boolean toAllUser, @NotNull IDingWorkNotifyMsg msg) {
        if (!toAllUser) {
            Preconditions.checkArgument(CollUtil.isNotEmpty(userIds) || CollUtil.isNotEmpty(deptIds),
                    "当toAllUser设置为false时必须指定userIds或deptIds其中一个参数的值。"
            );
        }
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList(CollUtil.isEmpty(userIds) ? null : String.join(",", userIds));
        request.setAgentId(app.getAgentId());
        request.setToAllUser(toAllUser);
        request.setMsg(msg.getMsg());
        request.setDeptIdList(CollUtil.isEmpty(deptIds) ? null : String.join(",", deptIds));
        
        return execute(
                SEND_WORK_NOTIFY,
                request,
                () -> "发送工作通知"
        );
    }
    
    @Override
    public OapiMessageCorpconversationAsyncsendV2Response sendWorkNotify(@NotNull List<String> userIds, List<String> deptIds, boolean toAllUser, @NotNull OapiMessageCorpconversationAsyncsendV2Request.Msg msg) {
        if (!toAllUser) {
            Preconditions.checkArgument(CollUtil.isNotEmpty(userIds) || CollUtil.isNotEmpty(deptIds),
                    "当toAllUser设置为false时必须指定userIds或deptIds其中一个参数的值。"
            );
        }
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList(CollUtil.isEmpty(userIds) ? null : String.join(",", userIds));
        request.setAgentId(app.getAgentId());
        request.setToAllUser(toAllUser);
        request.setMsg(msg);
        request.setDeptIdList(CollUtil.isEmpty(deptIds) ? null : String.join(",", deptIds));
        
        return execute(
                SEND_WORK_NOTIFY,
                request,
                () -> "发送媒体文件的工作通知"
        );
    }


    @Override
    public OapiMessageCorpconversationGetsendprogressResponse getWorkNotifyProgress(@NonNull Long taskId) {
        OapiMessageCorpconversationGetsendprogressRequest request = new OapiMessageCorpconversationGetsendprogressRequest();
        request.setAgentId(app.getAgentId());
        request.setTaskId(taskId);
        return execute(
                DingUrlConstant.WorkNotify.GET_WORK_NOTIFY_PROGRESS,
                request,
                () -> "获取工作通知发送进度"
        );
    }

    @Override
    public OapiMessageCorpconversationGetsendresultResponse getWorkNotifyResult(@NonNull Long taskId) {
        OapiMessageCorpconversationGetsendresultRequest request = new OapiMessageCorpconversationGetsendresultRequest();
        request.setAgentId(app.getAgentId());
        request.setTaskId(taskId);
        return execute(
                DingUrlConstant.WorkNotify.GET_WORK_NOTIFY_RESULT,
                request,
                () -> "获取工作通知发送结果"
        );
    }

    /**
     * 
     * @Description: 更新工作通知的状态 (https://open.dingtalk.com/document/orgapp/work-notice-option)
     * @see com.ly.dingtalk.service.message.IDingWorkNotifyMessageHandler#updateWorkNotify(java.lang.Long, java.lang.String, com.ly.dingtalk.service.message.IDingWorkNotifyMessageHandler.StatusBgColor)
     */
    @Override
    public OapiMessageCorpconversationStatusBarUpdateResponse updateWorkNotify(@NonNull Long taskId, @NonNull String statusValue, @NonNull StatusBgColor statusBgColor) {
        OapiMessageCorpconversationStatusBarUpdateRequest request = new OapiMessageCorpconversationStatusBarUpdateRequest();
        request.setAgentId(app.getAgentId());
        request.setStatusValue(statusValue);
        request.setStatusBg(String.valueOf(statusBgColor.getColor()));
        request.setTaskId(taskId);
        return execute(
                DingUrlConstant.WorkNotify.UPDATE_WORK_NOTIFY,
                request,
                () -> "更新工作通知"
        );
    }

}
